package com.situ.web.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.web.entity.PageInfo;
import com.situ.web.service.TeacherService;

@WebServlet("/teacher")
public class TeacherServlet extends HttpServlet{
	private TeacherService teacherService = new TeacherService();

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 解决POST传参乱码问题,一定要放在所有getParameter之前
		req.setCharacterEncoding("UTF-8");
		// /Java1912Web2/teacher?method=findByPage&pageNo=3&pageSize=5
		String method = req.getParameter("method");
		if (method == null || "".equals(method)) {
			method = "findByPage";
		}
		
		switch (method) {
		case "findByPage":
			findByPage(req, resp);
			break;
		case "findAll":
			findAll(req, resp);
			break;
		case "deleteById":
			deleteById(req, resp);
			break;
		default:
			break;
		}
	}

	private void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String pageNo = req.getParameter("pageNo");
		String pageSize = req.getParameter("pageSize");
		if (pageNo == null || "".equals(pageNo)) {
			pageNo = "1";
		}
		if (pageSize == null || "".equals(pageSize)) {
			pageSize = "5";
		}
		
		PageInfo pageInfo = teacherService.findByPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
		System.out.println(pageInfo);
		
		req.setAttribute("pageInfo", pageInfo);
		req.getRequestDispatcher("/teacher_page_list.jsp").forward(req, resp);
	}

	private void deleteById(HttpServletRequest req, HttpServletResponse resp) {
		// TODO Auto-generated method stub
		
	}

	private void findAll(HttpServletRequest req, HttpServletResponse resp) {
		// TODO Auto-generated method stub
		
	}
}
